package oracle.apps.financials.collection.wheel.widget;

import oracle.apps.financials.collection.R;
import oracle.apps.financials.collection.wheel.widget.adapter.AbstractWheelTextAdapter;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;

public class PopupWheelWindowListener implements OnClickListener {

	private Context context = null;
	private boolean scrolling = false;
	private String data[] = null;


	private PopupWindow popupWindow;

	public PopupWheelWindowListener(Context context, String data[]) {
		this.context = context;
		this.data = data;

	}

	@Override
	public void onClick(View parent) {
		LayoutInflater mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = mLayoutInflater.inflate(R.layout.picker_layout, null);
		initWheel(view);
		initPopupWindow(parent, view);
	}

	public void initWheel(View view) {
		WheelView wheelView = (WheelView) view.findViewById(R.id.picker_view_id);
		wheelView.setVisibleItems(3);
		wheelView.setViewAdapter(new WheelTextAdapter(context, data));
		wheelView.addChangingListener(new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				if (!scrolling) {
					System.out.println("--newValue=" + data[newValue]);
				}
			}
		});

		wheelView.addScrollingListener(new OnWheelScrollListener() {
			public void onScrollingStarted(WheelView wheel) {
				scrolling = true;
			}

			public void onScrollingFinished(WheelView wheel) {
				scrolling = false;

			}
		});
		wheelView.setCurrentItem(1);

	}

	public void dismissPopupWindow() {
		if (popupWindow != null) {
			popupWindow.dismiss();

		}
	}

	public void initPopupWindow(View parent, View view) {
		
		dismissPopupWindow() ;

		final PopupWindow popupWindow = new PopupWindow(view, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

		this.popupWindow = popupWindow;

		Button OkButton = (Button) view.findViewById(R.id.picker_view_ok);

		OkButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				popupWindow.dismiss();
			}
		});

		Button cancelButton = (Button) view.findViewById(R.id.picker_view_cancel);

		cancelButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				popupWindow.dismiss();
			}
		});

		popupWindow.setTouchInterceptor(new OnTouchListener() {

			@Override
			public boolean onTouch(View arg0, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
					popupWindow.dismiss();
					return true;
				}
				return false;
			}

		});
		popupWindow.setOutsideTouchable(true);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());

		popupWindow.showAsDropDown(parent);
		// showAtLocation(parent, Gravity.RIGHT | Gravity.BOTTOM, 0, 0);
	}

	private class WheelTextAdapter extends AbstractWheelTextAdapter {

		private String data[];

		/**
		 * Constructor
		 */
		protected WheelTextAdapter(Context context, String data[]) {
			super(context, R.layout.picker_item_layout, NO_RESOURCE);
			this.data = data;
			setItemTextResource(R.id.picker_item_name);
		}

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
			View view = super.getItem(index, cachedView, parent);
			return view;
		}

		@Override
		public int getItemsCount() {
			return data.length;
		}

		@Override
		protected CharSequence getItemText(int index) {
			return data[index];
		}
	}

}
